Skip to content

Conversation

@josepostiga
Copy link
Contributor

@josepostiga josepostiga commented Feb 2, 2025

Although the documentation for batch jobs clearly states that batched jobs should use the Batchable trait, there's not much that alerts or prevents the dev from wrongly trying to batch jobs that don't implement it, resulting in runtime problems. Even if we use the assertBatched test, tests pass even with invalid jobs supplied. I was a victim of this. (I know, it might also be a skill issue from my side, for forgetting to use the trait...)

This PR adds a check to batched jobs to ensure that the jobs use the required Batchable trait, throwing a RuntimeException if a job is found in a given list when calling Bus::batch or if supplied to the add method of a PendingBatch instance.

@josepostiga josepostiga marked this pull request as draft February 2, 2025 18:14
@josepostiga
Copy link
Contributor Author

josepostiga commented Feb 2, 2025

Looks like I missed a few things in this implementation. I'm looking into it... Converted the PR to draft, in the meantime.

Edit: I think we're good for review, now.

@josepostiga josepostiga marked this pull request as ready for review February 2, 2025 18:34
return;
}

if (! in_array(Batchable::class, class_uses_recursive($job))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it wouldn't be useful to cache the job's class so you don't have to call class_uses_recursive for multiple instances of the same class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's not possible with class_uses_recursive, this could be taken one step further, by implementing something similar to class_uses_recursive: Caching all classes in the recursive chain 🤔

@shaedrich
Copy link
Contributor

There might be cases, even though, it might be rare, where the trait isn't even used for some reason, but the functionality is implemented. This wouldn't work anymore. This could possibly be avoided by using an interface 🤔

@taylorotwell
Copy link
Member

This PR has unnecessary doc block changes.

@taylorotwell taylorotwell marked this pull request as draft February 5, 2025 10:46
@josepostiga
Copy link
Contributor Author

This PR has unnecessary doc block changes.

Reverted those automated changes.

@josepostiga josepostiga marked this pull request as ready for review February 5, 2025 11:02
@taylorotwell taylorotwell merged commit 928a709 into laravel:11.x Feb 5, 2025
16 of 38 checks passed
@josepostiga josepostiga deleted the fix/ensure-batched-jobs-are-batchable branch February 5, 2025 11:33
@mishkx
Copy link

mishkx commented Feb 12, 2025

What about batch callbacks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants